package com.netease.nie.controllers;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.base.exceptions.BusinessException;
import com.netease.nie.base.protocol.SystemResponseCode;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.FeedbackDto;
import com.netease.nie.service.FeedbackService;

/**
 * 意见反馈
 * 
 * @author wwbn3695
 * @createDate July 12th,2016
 * 
 */
@RestController
@RequestMapping("/api/user")
public class FeedbackController extends BaseController {

	@Autowired
	private FeedbackService feedbackService;

	@RequestMapping(value = "/feedback", method = RequestMethod.POST, produces = "application/json")
	public String feedback(HttpServletRequest request, @RequestBody FeedbackDto feedbackDto)
			throws Exception {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			throw new BusinessException(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		
		if (StringUtils.isBlank(feedbackDto.getContent()) || StringUtils.isBlank(feedbackDto.getContactWay()))
			throw new BusinessException(SystemResponseCode.PARAM_VALUE_ILLEGAL.getCode(), "用户id不合法");
		
		feedbackDto.setUserId(userId);
		int count = feedbackService.saveFeedbackDto(feedbackDto);
		return jsonSuccess(count);
	}
}
